﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLNS.Class;
namespace QLNS.NghiepVu
{
    public partial class frmGiaiQuyetNgiViec : Form
    {
        public frmGiaiQuyetNgiViec()
        {
            InitializeComponent();
        }
        DB cn = new DB();
        string tag = "";

        private void frmGiaiQuyetNgiViec_Load(object sender, EventArgs e)
        {
            load_button(tag );
            load_combo();
            load_grid();
        }
        private void load_grid()
        {
            cn.KetNoi();
            DataTable tbl = cn.getTable("SELECT ChiTietNghiViec.SoQD,ChiTietNghiViec.MaNhanVien, NhanVien.HoTen, NghiViec.HinhThucNghi, ChiTietNghiViec.MaNghiViec, ChiTietNghiViec.NgayBatDau, ChiTietNghiViec.NgayKetThuc, ChiTietNghiViec.LiDo FROM NhanVien INNER JOIN ChiTietNghiViec ON NhanVien.MaNhanVien = ChiTietNghiViec.MaNhanVien INNER JOIN NghiViec ON dbo.ChiTietNghiViec.MaNghiViec =NghiViec.MaNghiViec");
            dtgDanhSach.DataSource = tbl;

        }
        private void load_button(string tag)
        {

            if (tag == "them")
            {
                btnThem.Enabled = false;
                btnSua.Enabled = false;
                btnXoa.Enabled = false;

                btnLuu.Enabled = true;
                btnHuy.Enabled = true;
                txtLyDo.Enabled = true;
                txtLyDo.Text = "";


            }
            else if (tag == "sua")
            {
                btnThem.Enabled = false;
                btnSua.Enabled = false;
                btnXoa.Enabled = false;

                btnLuu.Enabled = true;
                btnHuy.Enabled = true;
            }
            else
            {
                btnThem.Enabled = true ;
                btnSua.Enabled = true ;
                btnXoa.Enabled = true ;

                btnLuu.Enabled = false ;
                btnHuy.Enabled = false ;
            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            tag = "them";
            cn.KetNoi();
            txtSoQD.Text = cn.SinhMa("SoQD","ChiTietNghiviec","NV");
            load_button(tag );
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            tag = "sua";
            load_button(tag );
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            DialogResult h = MessageBox.Show("Bạn có muốn xóa không?","Thông báo",MessageBoxButtons.YesNo,MessageBoxIcon.Question );
            if(h==DialogResult.Yes )
            {
                cn.KetNoi();
                cn.Delete_Table("ChiTietNghiViec","SoQD='"+txtSoQD.Text +"'");
                
            }
            MessageBox.Show("Xóa thành công!!");
            load_combo();
            load_grid();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (tag == "them")
            {
              
                cn.KetNoi();
                cn.Insert_Table("ChiTietNghiViec", "SoQD,MaNghiViec,MaNhanVien,NgayBatDau,NgayKetThuc,LiDo", "'" + txtSoQD.Text + "','" + cmbMaNghiViec.SelectedValue.ToString() + "','" + txtMaNhanVien.Text  + "','" + dtpNgayBatDau.Value.ToString("yyyy-MM-dd") + "','" + dtpNgayKetThuc.Value.ToString("yyyy-MM-dd") + "','" + txtLyDo.Text + "'");
            }
            else
            {
                cn.KetNoi();
                cn.Update_Table("ChiTietNghiViec", "MaNghiViec='" + cmbMaNghiViec.SelectedValue.ToString().Trim() + "',MaNhanVien='" + txtMaNhanVien.Text  + "',NgayBatDau='" + dtpNgayBatDau.Value.ToString("yyyy-MM-dd") + "',NgayKetThuc='" + dtpNgayKetThuc.Value.ToString("yyyy-MM-dd") + "',LiDo=N'" + txtLyDo.Text + "'", "SoQD='" + txtSoQD.Text + "'");
            }
            tag = "";
            load_button(tag );
            load_grid();
        }
        private void load_combo()
        {
            cn.KetNoi();

            cmbMaNghiViec.DataSource = cn.getTable("select *from NghiViec");
            cmbMaNghiViec.DisplayMember = "HinhThucNghi";
            cmbMaNghiViec.ValueMember = "MaNghiViec";

            cmbChucVu.DataSource = cn.getTable("select *from ChucVu");
            cmbChucVu.DisplayMember = "TenChucVu";
            cmbChucVu.ValueMember = "MaChucVu";

            

            cmbToMon.DataSource = cn.getTable("select *from ToMon");
            cmbToMon.DisplayMember = "TenToMon";
            cmbToMon.ValueMember = "MaToMon";
        }

        private void dtgDanhSach_SelectionChanged(object sender, EventArgs e)
        {
            if(dtgDanhSach.RowCount>0)
            {
                txtSoQD.Text=dtgDanhSach.CurrentRow.Cells["SoQD"].Value.ToString();
                cmbMaNghiViec.SelectedValue=dtgDanhSach.CurrentRow.Cells["MaNghiViec"].Value.ToString();
                dtpNgayBatDau.Value= DateTime.Parse(dtgDanhSach.CurrentRow.Cells["NgayBatDau"].Value.ToString());
                dtpNgayKetThuc.Value=DateTime.Parse(dtgDanhSach.CurrentRow.Cells["NgayKetThuc"].Value.ToString());
                txtLyDo.Text=dtgDanhSach.CurrentRow.Cells["LiDo"].Value.ToString();
                txtMaNhanVien.Text=dtgDanhSach.CurrentRow.Cells["MaNhanVien"].Value.ToString();
            }
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            tag = "";
            load_button(tag );
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            DialogResult h = MessageBox.Show("Bạn có muốn xóa không?","Thông báo",MessageBoxButtons.YesNo,MessageBoxIcon.Question );
            if(h==DialogResult.Yes )
            {
                this.Close();
            }
        }

        private void cmbToMon_SelectedValueChanged(object sender, EventArgs e)
        {
            //if(cmbToMon.SelectedIndex>-1)
            //{
             
            //    DataTable tbl = cn.g=etTable("SELECT NhanVien.MaNhanVien,NhanVien.HoTen FROM NhanVien INNER JOIN ToMon ON NhanVien.MaToMon = ToMon.MaToMon where MaToMon='"+cmbToMon.SelectedValue.ToString()+"'");
            //    dtgNhanVien.DataSource = tbl;
            if (cmbToMon.SelectedIndex > -1)
            {
                DataTable tbl = cn.getTable("select MaNhanVien,HoTen from NhanVien, ToMon where NhanVien.MaToMon=ToMon.MaToMon and ToMon.MaToMon='" + cmbToMon.SelectedValue.ToString().Trim() + "'");
                dtgNhanVien.DataSource = tbl;
            }
            else
            {
                txtMaNhanVien.Text = "";
                txtHoTen.Text = "";
            }
            //}
        }

        private void dtgNhanVien_SelectionChanged(object sender, EventArgs e)
        {
            if (dtgNhanVien.RowCount > 0)
            {
                txtMaNhanVien.Text = dtgNhanVien.CurrentRow.Cells["MaNV"].Value.ToString();
                txtHoTen.Text = dtgNhanVien.CurrentRow.Cells["TenVN"].Value.ToString();
            }
            else
            {
                
            }
        }
    }
}
